package com.example.walkingtour.list;

import java.io.File;
import com.example.walkingtour.data.*;
import com.example.walkingtour.*;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.CursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class TourList extends ListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {

	private static final String TAG = "TourList";

	private static final int WT_LOADER_ID = 0;
	private MySimpleCursorAdapter mAdapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// this is really important in order to save the state across screen
		// configuration changes for example
		setRetainInstance(true);
		Log.d(TAG, "onActivityCreated(...)");

		registerForContextMenu(getListView());
		// int count = getListView().getCount();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.list_layout, null);

		String[] columnData = new String[] { WTDatabase.COL_FILE,
				WTDatabase.COL_GPS_LAT, WTDatabase.COL_GPS_LONG };

		int[] viewIDs = new int[] { R.id.wtFile, R.id.wtGPS_Lat,
				R.id.wtGPS_Long };
		/*
		 * setting cursor as null until actual cursor is received from loader
		 * manager
		 */
		mAdapter = new MySimpleCursorAdapter(this.getActivity(),
				R.layout.walking_tour_item, null, columnData, viewIDs,
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

		// Associate the mAdapter with the ListView.
		setListAdapter(mAdapter);

		// Initialize the Loader with id LOADER_ID, and this activity.
		// As the Loader sees changes in the data, these changes will be
		// reported back to this activity through the LoaderCallbacks interface
		LoaderManager loaderManager = this.getLoaderManager();
		loaderManager.initLoader(WT_LOADER_ID, null, this);

		setListAdapter(mAdapter);

		return view;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = this.getActivity().getMenuInflater();
		inflater.inflate(R.menu.play_context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_playItem:
			File myFile = WalkingTourHelper.getFileById(info.id,
					this.getActivity());
			String fullpath = myFile.getAbsolutePath();
			int action = WalkingTourHelper.getActionByPath(fullpath);

			switch (action) {
			case WalkingTourHelper.ACTION_TAKE_VIDEO:
				PlayVideoActivity.playVideo(this.getActivity(), fullpath);
				break;
			default:
				Toast.makeText(this.getActivity(), "Error reading file",
						Toast.LENGTH_LONG).show();
			}

			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		// We create the CursorLoader object here using the content URI. This
		// loader object will pull columns based on the PROJECTION.
		return new CursorLoader(this.getActivity(), WTProvider.CONTENT_URI,
				WalkingTourHelper.COLUMN_NAMES, null, null, null);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		mAdapter.swapCursor(arg1);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mAdapter.swapCursor(null);
	}

}
